<template>
  <div class="home-music-container">
    <!-- 主内容区域 - 歌曲选择 -->
    <div class="main-content-wrapper">
      <div class="main-content">

        <!-- 推荐内容 -->
        <div class="recommend-section">
          <div class="section-header">
            <h2>热门推荐</h2>
            <el-link type="primary" :underline="false" @click="viewMore">查看更多</el-link>
          </div>

          <div class="album-grid">
            <div class="album-card" v-for="(album, index) in albums" :key="index">
              <div class="album-cover">
                <img :src="album.cover" alt="{{ album.name }}">
                <div class="play-btn">
                  <el-icon>
                    <Play />
                  </el-icon>
                </div>
              </div>
              <h3>{{ album.name }}</h3>
              <p>{{ album.artist }}</p>
            </div>
          </div>
        </div>

        <!-- 热门歌曲 -->
        <div class="hot-songs-section">
          <div class="section-header">
            <h2>热门歌曲</h2>
          </div>

          <el-table :data="hotSongs" style="width: 100%" :header-cell-style="headerCellStyle">
            <el-table-column prop="rank" label="" width="50" align="center"></el-table-column>
            <el-table-column prop="name" label="歌曲" min-width="200">
              <template #default="scope">
                <div class="song-info">
                  <span class="song-name">{{ scope.row.name }}</span>
                  <el-icon v-if="scope.row.mv" class="mv-icon">
                    <VideoPlay />
                  </el-icon>
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="artist" label="歌手" min-width="150"></el-table-column>
            <el-table-column prop="album" label="专辑" min-width="200"></el-table-column>
            <el-table-column label="操作" width="100" fixed="right">
              <template #default="scope">
                <el-button type="text" :icon="Play" @click="playSong(scope.row)"></el-button>
                <el-dropdown trigger="click">
                  <el-button type="text" :icon="MoreFilled"></el-button>
                  <template #dropdown>
                    <el-dropdown-menu>
                      <el-dropdown-item>添加到播放列表</el-dropdown-item>
                      <el-dropdown-item>收藏</el-dropdown-item>
                      <el-dropdown-item>分享</el-dropdown-item>
                    </el-dropdown-menu>
                  </template>
                </el-dropdown>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { ElMessage } from 'element-plus'
import api from '@/utils/api.js'
import axios from 'axios'
// import {
//   HomeFilled, Music, VideoPlay, Collection, Download, Search,
//   Play, Pause, StepBackward, StepForward, Shuffle, RefreshRight,
//   LikeFilled, List, Volume, MoreFilled
// } from 'element-plus/icons-vue'

// 搜索相关
const searchQuery = ref('')

// 播放状态
const isPlaying = ref(false)
const currentSong = ref(true)
const currentTime = ref(0)
const totalTime = ref(237)
const progress = ref(0)
const volume = ref(70)
const token = ref(localStorage.getItem("token"))

// 示例数据 - 热门专辑
const albums = reactive([
  { id: 1, name: '华语流行热歌', artist: 'Various Artists', cover: 'https://picsum.photos/seed/album1/300/300' },
  { id: 2, name: '欧美金曲', artist: 'Various Artists', cover: 'https://picsum.photos/seed/album2/300/300' },
  { id: 3, name: '电音派对', artist: 'Various Artists', cover: 'https://picsum.photos/seed/album3/300/300' },
  { id: 4, name: '轻音乐', artist: 'Various Artists', cover: 'https://picsum.photos/seed/album4/300/300' },
  { id: 5, name: '经典老歌', artist: 'Various Artists', cover: 'https://picsum.photos/seed/album5/300/300' },
  { id: 6, name: '摇滚现场11', artist: 'Various Artists', cover: 'https://picsum.photos/seed/album6/300/300' }
])

// 示例数据 - 热门歌曲
const hotSongs = reactive([
  { rank: 1, name: '起风了', artist: '买辣椒也用券', album: '起风了', mv: true },
  { rank: 2, name: '世界这么大还是遇见你', artist: '程响', album: '世界这么大还是遇见你', mv: true },
  { rank: 3, name: '这世界那么多人', artist: '莫文蔚', album: '这世界那么多人', mv: true },
  { rank: 4, name: '孤勇者', artist: '陈奕迅', album: '孤勇者', mv: true },
  { rank: 5, name: '爱你', artist: '王心凌', album: '爱你', mv: true },
  { rank: 6, name: '如愿', artist: '王菲', album: '如愿', mv: true },
  { rank: 7, name: '晚风', artist: '陈婧霏', album: '晚风', mv: false },
  { rank: 8, name: '一路向北', artist: '周杰伦', album: '十一月的萧邦', mv: true },
  { rank: 9, name: '成都', artist: '赵雷', album: '无法长大', mv: true },
  { rank: 10, name: '像我这样的人', artist: '毛不易', album: '平凡的一天', mv: true }
])

// 样式计算属性
const inputStyle = computed(() => ({
  borderRadius: '20px',
  borderColor: '#dcdfe6',
  '&:hover': {
    borderColor: '#c0c4cc'
  },
  '&.is-focus': {
    borderColor: '#8294c4',
    boxShadow: '0 0 0 2px rgba(130, 148, 196, 0.2)'
  }
}))

const buttonStyle = {
  borderRadius: '20px',
  backgroundColor: '#8294c4',
  borderColor: '#8294c4',
  '&:hover': {
    backgroundColor: '#6b82b6',
    borderColor: '#6b82b6'
  }
}

const playButtonStyle = computed(() => ({
  backgroundColor: '#8294c4',
  borderColor: '#8294c4',
  '&:hover': {
    backgroundColor: '#6b82b6',
    borderColor: '#6b82b6',
    transform: 'scale(1.05)'
  },
  transition: 'all 0.3s ease'
}))

const headerCellStyle = computed(() => ({
  backgroundColor: '#f5f7fa',
  fontSize: '14px',
  color: '#606266'
}))

// 搜索处理
function handleSearch() {
  if (searchQuery.value.trim()) {
    ElMessage.info(`搜索: ${searchQuery.value}`)
    // 实际应用中这里应该调用搜索API
  }
}

// 查看更多
function viewMore() {
  ElMessage.info('查看更多推荐内容')
}

// 播放歌曲
function playSong(song) {
  currentSong.value = {
    ...song,
    cover: `https://picsum.photos/seed/${song.id}/300/300`
  }
  isPlaying.value = true
  ElMessage.success(`正在播放: ${song.name} - ${song.artist}`)
}

// 切换播放状态
function togglePlay() {
  isPlaying.value = !isPlaying.value
  if (isPlaying.value && !currentSong.value) {
    // 如果当前没有播放的歌曲，默认播放第一首热门歌曲
    playSong(hotSongs[0])
  }
}

// 格式化时间
function formatTime(seconds) {
  const mins = Math.floor(seconds / 60)
  const secs = Math.floor(seconds % 60)
  return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`
}

// 格式化进度条提示
function formatTooltip(value) {
  return ''
}

// 进度条变化处理
function onProgressChange(value) {
  currentTime.value = Math.floor((value / 100) * totalTime.value)
}

// 音量变化处理
function onVolumeChange(value) {
  // 实际应用中这里应该调节音量
  ElMessage.info(`音量已调整为: ${value}%`)
}
</script>

<style scoped>
/* 全局样式重置和变量 */
:root {
  --primary-color: #8294c4;
  --primary-hover: #6b82b6;
  --sidebar-bg: #1a1a1a;
  --main-bg: #f5f7fa;
  --card-bg: #ffffff;
  --text-primary: #333333;
  --text-secondary: #606266;
  --text-muted: #999999;
  --border-color: #e4e7ed;
  --shadow-light: 0 2px 8px rgba(0, 0, 0, 0.08);
}

/* 整体容器 */
.home-music-container {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  background-color: var(--main-bg);
  overflow: hidden;
}

/* 主内容区域样式 */
.main-content-wrapper {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.main-content {
  flex: 1;
  padding: 20px;
  background-color: #ffffff;
  height: calc(100vh - 140px);
  border-radius: 8px;
  /* 减去顶部导航和底部播放器的高度 */
  overflow-y: auto;
  /* 允许上下滑动 */
  overflow-x: hidden;
  /* 防止出现水平滚动条 */
}

/* 搜索栏样式 */
.search-bar {
  margin-bottom: 20px;
  display: flex;
  justify-content: center;
}

.search-bar .el-input {
  width: 600px;
  max-width: 100%;
}

/* 区域头部样式 */
.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 25px;
}

.section-header h2 {
  color: var(--text-primary);
  font-size: 24px;
  font-weight: 600;
  margin: 0;
  position: relative;
}

.section-header h2::after {
  content: '';
  position: absolute;
  left: 0;
  bottom: -8px;
  width: 40px;
  height: 3px;
  background-color: var(--primary-color);
  border-radius: 1.5px;
}

/* 专辑网格样式 */
.album-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 30px;
  margin-bottom: 60px;
}

.album-card {
  cursor: pointer;
  transition: all 0.3s ease;
  background-color: var(--card-bg);
  border-radius: 8px;
  padding: 15px;
  box-shadow: var(--shadow-light);
}

.album-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

.album-cover {
  position: relative;
  margin-bottom: 15px;
  overflow: hidden;
  border-radius: 4px;
}

.album-cover img {
  width: 100%;
  height: auto;
  display: block;
  transition: transform 0.5s ease;
}

.album-card:hover .album-cover img {
  transform: scale(1.05);
}

.play-btn {
  position: absolute;
  bottom: 10px;
  right: 10px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: var(--primary-color);
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  opacity: 0;
  transition: all 0.3s ease;
  transform: translateX(10px);
}

.album-card:hover .play-btn {
  opacity: 1;
  transform: translateX(0);
}

.play-btn:hover {
  background-color: var(--primary-hover);
  transform: scale(1.1);
}

.album-card h3 {
  font-size: 14px;
  color: var(--text-primary);
  margin: 8px 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-weight: 500;
}

.album-card p {
  font-size: 12px;
  color: var(--text-muted);
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 热门歌曲表格样式 */
.hot-songs-section {
  background-color: var(--card-bg);
  border-radius: 8px;
  padding: 25px;
  box-shadow: var(--shadow-light);
}

.song-info {
  display: flex;
  align-items: center;
}

.song-name {
  font-size: 14px;
  color: var(--text-primary);
}

.mv-icon {
  margin-left: 8px;
  color: var(--primary-color);
  font-size: 14px;
}

/* 用户信息容器样式 */
.user-info-container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 4px;
  transition: background-color 0.3s ease;
}

.user-info-container:hover {
  width: 100%;
  background-color: rgba(130, 148, 196, 0.1);
}

.user-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  margin-right: 8px;
  border: 2px solid #f0f0f0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.user-info-container:hover .user-avatar {
  transform: scale(1.05);
  border-color: var(--primary-color);
}

.user-name {
  font-size: 14px;
  color: var(--text-primary);
  font-weight: 500;
}

/* 滚动条样式优化 */
.main-content::-webkit-scrollbar {
  width: 6px;
}

.main-content::-webkit-scrollbar-track {
  background: transparent;
}

.main-content::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.1);
  border-radius: 3px;
}

.main-content::-webkit-scrollbar-thumb:hover {
  background: rgba(130, 148, 196, 0.3);
}

/* 响应式调整 */
@media (max-width: 1200px) {
  .album-grid {
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 25px;
  }
}

@media (max-width: 1024px) {
  .search-bar .el-input {
    width: 100%;
  }

  .album-grid {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 20px;
  }
}

@media (max-width: 768px) {
  .main-content {
    padding: 20px 15px;
  }

  .section-header h2 {
    font-size: 20px;
  }

  .album-grid {
    grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
    gap: 15px;
  }
}

@media (max-width: 480px) {
  .album-grid {
    grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
    gap: 10px;
  }

  .album-card {
    padding: 10px;
  }
}
</style>